package com.hui.xiao.xiaohui.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.TableLayout;
import android.widget.TextView;

import com.hui.xiao.xiaohui.R;

/**
 * Created by Administrator on 2016/5/22.
 */
public class DialogTest extends Activity {

    TextView show;
    String[] items = new String[]{
            "疯狂Java讲义", "疯狂Ajax讲义",
            "轻量级Java EE企业应用实战",
            "疯狂Android讲义"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog);
        show = (TextView) findViewById(R.id.show);
    }

    public void simple(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("简单对话框")
                .setIcon(R.drawable.bkch)
                .setMessage("我爱你爱着你\n就像老鼠爱大米");
        setPositiveButton(builder);
        setNegativeButton(builder)
                .create()
                .show();
    }

    private AlertDialog.Builder setPositiveButton(AlertDialog.Builder builder) {
        return builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                show.setText("你单机了确定按钮");
            }
        });
    }

    private AlertDialog.Builder setNegativeButton(AlertDialog.Builder builder) {
        return builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                show.setText("单机了取消按钮");
            }
        });
    }

    public void simpleList(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("简单列表对话框")
                .setIcon(R.drawable.aboc_s)
                .setItems(R.array.books, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        show.setText("你选中了" + which);
                    }
                });
        setPositiveButton(builder);
        setNegativeButton(builder)
                .create()
                .show();
    }

    public void multiChoice(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("多选列表对话框")
                .setIcon(R.drawable.aboc_s)
                .setMultiChoiceItems(R.array.books, new boolean[]{false, true, false, true}, null);
        setPositiveButton(builder);
        setNegativeButton(builder)
                .create().show();

    }

    public void customList(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("自定义列表对话框")
                .setIcon(R.drawable.aboc_s)
                .setAdapter(new ArrayAdapter<String>(this, R.layout.array_item, items), null);
        setPositiveButton(builder);
        setNegativeButton(builder)
                .create().show();
    }

    public void customView(View view) {
        TableLayout loginForm = (TableLayout) getLayoutInflater().inflate(R.layout.login, null);
        new AlertDialog.Builder(this)
                .setIcon(R.drawable.aboc_s)
                .setTitle("自定义view对话框")
                .setView(loginForm)
                .setPositiveButton("登录", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        show.setText("登录了" + which);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        show.setText("取消登录了" + which);
                    }
                })
                .create()
                .show();

    }
}
